package cloud.xlh.test.controller;

import cloud.xlh.infrastructure.utils.FileUtil;
import cloud.xlh.infrastructure.utils.RsaUtil;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : xlh_osms
 * @ Author: XuLeHuang
 * @ Date: 2023/2/1 10:35
 * @ Description:
 */
@RestController
@RequestMapping("/gen")
public class GenController {

    @Value("${keyPropertiesLocation}")
    private String keyPropertiesLocation;

    @RequestMapping("/genPasswordByRsaWithBase64")
    public String genPasswordByRsaWithBase64(String originalPassword) throws UnsupportedEncodingException {
        String str = originalPassword + UUID.randomUUID().toString().substring(0, 3);
        String encryptedPasswordByRsa = "";
        try {
            Properties properties = FileUtil.readKeyPropertiesFile(FileUtil.PathTypeCode.ABSOLUTE_PATH.getCode(), keyPropertiesLocation , false);
            String rsaPublicKey = properties.getProperty("rsaPublicKey");
            encryptedPasswordByRsa = RsaUtil.encrypt(str, rsaPublicKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new String(Base64.encodeBase64(encryptedPasswordByRsa.getBytes()), "UTF-8");
    }
}
